<template>
  <div class="edit">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{title}}</span>
        <el-button @click="$router.go(-1)" style="float: right; padding: 3px 0" type="text">返回</el-button>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <!-- <el-form-item label="发布时间" prop="icon">
          <el-date-picker v-model="value1" type="createTime" placeholder="选择发布时间"></el-date-picker>
        </el-form-item>-->
        <el-form-item label="内容详情" prop="content">
          <Editor v-model="form.content" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">确定</el-button>
          <el-button @click="cancel">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import Editor from "@/components/Editor";
import {
  getResearchHistory,
  addResearchHistory,
  updateResearchHistory
} from "@/api/system/researchHistory";

export default {
  name: "SuccessCaseEdit",
  components: {
    Editor
  },
  data() {
    return {
      title: "添加研发历史",
      // 表单参数
      form: {
        title: "",
        sort: 0,
        content: ""
      },
      // 表单校验
      rules: {
        title: [{ required: true, message: "请输入标题" }]
      }
    };
  },
  mounted() {
    this.getEditInfo();
  },
  methods: {
    getEditInfo() {
      const id = this.$route.query.id;
      if (id) {
        getResearchHistory(id).then(response => {
          this.form = response.data;
        });
        this.title = "修改研发历史";
      }
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const postData = {
            ...this.form,
            content: this.filterContent(this.form.content)
          };
          if (this.form.id != null) {
            updateResearchHistory(postData).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.$router.go(-1);
              }
            });
          } else {
            addResearchHistory(postData).then(response => {
              if (response.code === 200) {
                this.msgSuccess("添加成功");
                this.$router.go(-1);
              }
            });
          }
        }
      });
    },
    // 重置按钮
    cancel() {
      this.getEditInfo();
      this.resetForm("form");
    }
  }
};
</script>
<style lang="scss" scoped>
.item {
  font-size: 14px;
  padding: 18px 0;
  span:first-child {
    width: 90px;
    display: inline-block;
    text-align: right;
  }
  span:last-child {
    margin-left: 20px;
    display: inline-block;
  }
  .image {
    width: 100px;
    height: 100px;
    margin-right: 20px;
  }
}
.deatil {
  display: flex;
  .avatar {
    width: 140px;
    height: 140px;
    margin-right: 20px;
    .img {
      height: 100%;
    }
    /deep/ img {
      border-radius: 50%;
    }
  }
  .info {
    flex: 1;
  }
}
</style>
